<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui">

    <ui:composition template="/template.xhtml">
        <f:metadata>
            <f:event listener="#{tbInstController.setSelectedEvento(null)}" type="preRenderComponent" id="fevent"/>
        </f:metadata>  
        <ui:define name="body">
            <h:form id="TbInstListForm">
                <p:breadCrumb>
                    <p:menuitem value="Home" url="#" />
                    <p:menuitem value="Consulta" url="#" />
                    <p:menuitem value="Cliente" url="#" />
                </p:breadCrumb>   
                <p:panel>
                    <p:dataTable id="datalist"
                                 emptyMessage="Nenhum registro localizado"
                                 scrollable="true"
                                 scrollWidth="98%"
                                 widgetVar="FiltroGlobal"
                                 value="#{tbInstController.items}" 
                                 var="item"
                                 selectionMode="single" 
                                 selection="#{tbInstController.selected}"
                                 paginator="true"
                                 rowKey="#{item.codInstituicao}"
                                 rows="10"
                                 paginatorPosition="bottom"
                                 rowsPerPageTemplate="10,20,30,40,50">
                        
                        <p:ajax event="rowSelect"   update="buttonFicha"/>
                        <p:ajax event="rowUnselect" update="buttonFicha"/>
                        
                        <f:facet name="header">
                            <p:outputPanel>
                                <p:inputText id="globalFilter" style="width:200px;" placeholder=""/>
                                <p:commandButton value="Filtrar" id="withIcon" onclick="PF('FiltroGlobal').filter()" icon="ui-icon-search" />
                                <p:commandButton id="toggler" type="button" value="Colunas" style="float:right" icon="ui-icon-calculator" rendered="false" />
                                <p:columnToggler datasource="datalist" trigger="toggler" />

                                <p:splitButton value="Exportar" icon="ui-icon-disk">
                                    <p:menuitem value="PDF"  icon="ui-icon-close"  ajax="false"><p:dataExporter type="pdf" target="datalist" pageOnly="true" preProcessor="#{tbInstController.preProcessPDF}"  encoding="UTF-8" fileName="Clientes" /></p:menuitem>
                                    <p:menuitem value="XLS"  icon="ui-icon-close"  ajax="false"><p:dataExporter type="xls" target="datalist" pageOnly="true" fileName="Clientes" /></p:menuitem>
                                    <p:menuitem value="CSV"  icon="ui-icon-close"  ajax="false"><p:dataExporter type="csv" target="datalist" pageOnly="true" fileName="Clientes" /></p:menuitem>    
                                    <p:menuitem value="XML"  icon="ui-icon-close"  ajax="false"><p:dataExporter type="xml" target="datalist" pageOnly="true" fileName="Clientes" /></p:menuitem>                                
                                </p:splitButton>
                            </p:outputPanel>
                        </f:facet>                       
                        <p:column filterBy="#{item.codInstituicao}" filterMatchMode="contains" style="width:180px;">
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_codInstituicao}"/>
                            </f:facet>
                            <h:outputText value="#{item.codInstituicao}"/>
                        </p:column>
                         <p:column filterBy="#{item.nomFantasia}" filterMatchMode="contains" style="width:180px;"> 
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_nomFantasia}"/>
                            </f:facet>
                            <h:outputText value="#{item.nomFantasia}"/>
                        </p:column>
                        <p:column filterBy="#{item.desFone1.replaceAll('\\(|\\)|\\-| ','')}" filterMatchMode="contains" style="width:180px;"> 
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_desFone1}"/>
                            </f:facet>
                            <h:outputText value="#{item.desFone1}"/>
                        </p:column>
                        <p:column filterBy="#{item.desEmail}" filterMatchMode="contains" style="width:180px;"> 
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_desEmail}"/>
                            </f:facet>
                            <h:outputText value="#{item.desEmail}"/>
                        </p:column>
                        <p:column filterBy="#{item.desCnpjCpf}" filterMatchMode="contains" style="width:180px;">  
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_desCnpjCpf}"/>
                            </f:facet>
                            <h:outputText value="#{item.desCnpjCpf}"/>
                        </p:column>
                        <p:column filterBy="#{tbInstController.customFormatDate(item.datNascimentoFundacao)}" filterFunction="#{tbInstController.filterByDateNascimento}"  filterMatchMode="contains" style="width:190px">
                            <f:facet name="filter" >
                                 <h:inputHidden />
                            </f:facet>
                            <f:facet name="header">
                                 <h:outputText value="Dt Nasc / Fund"/><br />
                                 <label style="padding-right: 4px;">De</label>
                                 <p:calendar id="fromNascimento" value="#{tbInstController.dateFromNascimento}" styleClass="customCalendar" pattern="dd/MM/yyyy" mask="99/99/9999">
                                     <p:ajax event="dateSelect" oncomplete="PF('FiltroGlobal').filter()" />
                                     <p:ajax event="change" oncomplete="PF('FiltroGlobal').filter()" />
                                 </p:calendar>
                                 <br /><label style="padding-right: 10px">A</label>
                                 <p:calendar id="toNascimento" value="#{tbInstController.dateToNascimento}" styleClass="customCalendar" pattern="dd/MM/yyyy" mask="99/99/9999">
                                     <p:ajax event="dateSelect" oncomplete="PF('FiltroGlobal').filter()"/>
                                     <p:ajax event="change" oncomplete="PF('FiltroGlobal').filter()" />
                                 </p:calendar>
                             </f:facet>                            
                            <h:outputText value="#{tbInstController.customFormatDate(item.datNascimentoFundacao)}" />
                        </p:column>
                        <p:column filterBy="#{interessesController.findByClienteId(item)}" filterFunction="#{tbInstController.filterByInteresse}" filterMatchMode="contains" style="width:180px;">  
                            <f:facet name="header">
                                <h:outputText value="Interesses"/>
                            </f:facet>
                            <p:selectOneListbox id="basic" value="#{item.interessesList}">
                                <f:selectItems value="#{interessesController.findByClienteId(item)}" var="inter" itemLabel="#{inter.descricao}" itemValue="#{inter.id}" />                                
                            </p:selectOneListbox>                            
                        </p:column>
                        <p:column filterBy="#{item.desEstadocivil}" filterMatchMode="contains" style="width:180px;">  
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_desEstadocivil}"/>
                            </f:facet>
                            <h:outputText value="#{item.desEstadocivil}"/>
                        </p:column>
                        <p:column filterBy="#{item.desSexo}" filterMatchMode="contains" style="width:180px;">  
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_desSexo}"/>
                            </f:facet>
                            <h:outputText value="#{item.desSexo}"/>
                        </p:column>
                        <p:column filterBy="#{item.profissaoProfissaoId.descricao}" filterMatchMode="contains" style="width:180px;">  
                            <f:facet name="header">
                                <h:outputText value="Profissão"/>
                            </f:facet>
                            <h:outputText value="#{item.profissaoProfissaoId.descricao}"/>
                        </p:column>
                        <p:column filterBy="#{item.profissaoFormacaoId.descricao}" filterMatchMode="contains" style="width:180px;">  
                            <f:facet name="header">
                                <h:outputText value="Formação"/>
                            </f:facet>
                            <h:outputText value="#{item.profissaoFormacaoId.descricao}"/>
                        </p:column>
                        <p:column filterBy="#{item.desLideranca.equals('S') ? 'S': 'N'}" filterMatchMode="equals" style="width:180px;">  
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_desLideranca}"/>
                            </f:facet>
                            <f:facet name="filter">
                                <p:selectOneButton onchange="PF('FiltroGlobal').filter()">
                                    <f:selectItem itemLabel="Todos" itemValue="" />
                                    <f:selectItem itemLabel="Sim" itemValue="S" />
                                    <f:selectItem itemLabel="Não" itemValue="N" />
                                </p:selectOneButton>
                            </f:facet>
                            <h:outputText value="#{item.desLideranca.equals('S') ? 'Sim': 'Não'}"/>
                        </p:column>
                        <p:column filterBy="#{item.desRazaosocial}" filterMatchMode="contains" style="width:180px;"> 
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_desRazaosocial}"/>
                            </f:facet>
                            <h:outputText value="#{item.desRazaosocial}"/>
                        </p:column>
                        <p:column filterBy="#{item.subcategoriaId.categoriaId.descricao}" filterMatchMode="contains" style="width:180px;">  
                            <f:facet name="header">
                                <h:outputText value="Categoria"/>
                            </f:facet>
                            <h:outputText value="#{item.subcategoriaId.categoriaId.descricao}"/>
                        </p:column>
                        <p:column filterBy="#{item.subcategoriaId.descricao}" filterMatchMode="contains" style="width:180px;">  
                            <f:facet name="header">
                                <h:outputText value="Subcategoria"/>
                            </f:facet>
                            <h:outputText value="#{item.subcategoriaId.descricao}"/>
                        </p:column>
                        <p:column filterBy="#{item.desEndereco}" filterMatchMode="contains" style="width:180px;">  
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_desEndereco}"/>
                            </f:facet>
                            <h:outputText value="#{item.desEndereco}"/>
                        </p:column>
                        <p:column filterBy="#{item.desCidade}" filterMatchMode="contains" style="width:180px;">  
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_desCidade}"/>
                            </f:facet>
                            <h:outputText value="#{item.desCidade}"/>
                        </p:column>
                        <p:column filterBy="#{item.desUf}" filterMatchMode="contains" style="width:180px;">  
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_desUf}"/>
                            </f:facet>
                            <h:outputText value="#{item.desUf}"/>
                        </p:column>
                        <p:column filterBy="#{item.desPais}" filterMatchMode="contains" style="width:180px;" exportable="false">  
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_desPais}"/>
                            </f:facet>
                            <h:outputText value="#{item.desPais}"/>
                        </p:column>
                        <p:column filterBy="#{item.desNewsletter.equals('S') ? 'S': 'N'}" filterMatchMode="equals" style="width:180px;" exportable="false">  
                            <f:facet name="header">
                                <h:outputText value="Rec. Informações"/>
                            </f:facet>
                            <f:facet name="filter">
                                <p:selectOneButton onchange="PF('FiltroGlobal').filter()">
                                    <f:selectItem itemLabel="Sim" itemValue="S" />
                                    <f:selectItem itemLabel="Não" itemValue="N" />
                                </p:selectOneButton>
                            </f:facet>
                            <h:outputText value="#{item.desNewsletter.equals('S') ? 'Sim': 'Não'}"/>
                        </p:column>
                        <p:column filterBy="#{tbInstController.customFormatDate(item.datInclusao)}" filterFunction="#{tbInstController.filterByDate}"  filterMatchMode="contains" style="width:190px">
                            <f:facet name="filter" >
                                 <h:inputHidden />
                            </f:facet>
                            <f:facet name="header">
                                 <h:outputText value="Data Inclusão"/><br />
                                 <label style="padding-right: 4px;">De</label>
                                 <p:calendar id="from" value="#{tbInstController.dateFrom}" styleClass="customCalendar" pattern="dd/MM/yyyy" mask="99/99/9999">
                                     <p:ajax event="dateSelect" oncomplete="PF('FiltroGlobal').filter()" />
                                     <p:ajax event="change" oncomplete="PF('FiltroGlobal').filter()" />
                                 </p:calendar>
                                 <br /><label style="padding-right: 10px">A</label>
                                 <p:calendar id="to" value="#{tbInstController.dateTo}" styleClass="customCalendar" pattern="dd/MM/yyyy" mask="99/99/9999">
                                     <p:ajax event="dateSelect" oncomplete="PF('FiltroGlobal').filter()"/>
                                     <p:ajax event="change" oncomplete="PF('FiltroGlobal').filter()" />
                                 </p:calendar>
                             </f:facet>                            
                            <h:outputText value="#{tbInstController.customFormatDate(item.datInclusao)}" />
                        </p:column> 

                        <f:facet name="footer">    
                            <p:splitButton value="Exportar" icon="ui-icon-disk">
                                <p:menuitem value="PDF"  icon="ui-icon-close"  ajax="false"><p:dataExporter type="pdf" target="datalist" pageOnly="true" preProcessor="#{tbInstController.preProcessPDF}"  encoding="UTF-8" fileName="Clientes" /></p:menuitem>
                                <p:menuitem value="XLS"  icon="ui-icon-close"  ajax="false"><p:dataExporter type="xls" target="datalist" pageOnly="true" fileName="Clientes" /></p:menuitem>
                                <p:menuitem value="CSV"  icon="ui-icon-close"  ajax="false"><p:dataExporter type="csv" target="datalist" pageOnly="true" fileName="Clientes" /></p:menuitem>    
                                <p:menuitem value="XML"  icon="ui-icon-close"  ajax="false"><p:dataExporter type="xml" target="datalist" pageOnly="true" fileName="Clientes" /></p:menuitem>
                            </p:splitButton>
                            <p:commandButton id="buttonFicha" value="Ficha" update=":CliViewFichaDialogForm" oncomplete="PF('CliViewFichaDialog').show()" icon="ui-icon-search" title="Ficha" style="margin:0" disabled="#{empty tbInstController.selected}">
                                <f:setPropertyActionListener value="#{tbInstController.selected}" target="#{sacController.selectedCliente}" />
                                <f:setPropertyActionListener value="#{tbInstController.selected}" target="#{visitasController.selectedCliente}" />
                                <f:setPropertyActionListener value="#{tbInstController.selected}" target="#{sigifEventosintController.selectedCliente}" />
                            </p:commandButton>                            
                        </f:facet>                       
                    </p:dataTable>
                </p:panel>
            </h:form>
            <ui:include src="../tbInst/ViewFicha.xhtml"/>
        </ui:define>
    </ui:composition>

</html>
